package com.xingkeduo.pojo.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.ServprodQuery;
import com.xingkeduo.pojo.table.ServprodTable;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

public class ServprodJson extends BaseJson {

	public static ServprodQuery createQueryForServprod(String json) throws Throwable {
		ServprodQuery query = new ServprodQuery();
		JsonNode root = validJson(json);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");
		query.setId(validId(root, YesNoEnum.YES));
		return query;
	}

	public static ServprodTable createTableForModifyServprod(String urlParam, ServprodTable servprodTable) throws Throwable {
		JsonNode root = validJson(urlParam);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");
		servprodTable.setId(validId(root, YesNoEnum.YES));
		return servprodTable;
	}

	//正常

    //查看输出
    public static ObjectNode toJson(ServprodTable servprodTable) {
        ObjectNode root = MAPPER.createObjectNode();

        if (servprodTable != null)
        {
            root.put("id", servprodTable.getId());
            root.put("code", servprodTable.getCode());
            root.put("name", servprodTable.getName());
            root.put("intro", servprodTable.getIntro());
            root.put("sort", servprodTable.getSort());
            //处理时间
			String createdTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(servprodTable.getCreatedTime());
            root.put("createtime", createdTime);
        }
        return root;
    }

    public static ArrayNode toJson(List<ServprodTable> servprodTableList) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if(!CommonUtil.isEmpty(servprodTableList)){
            for (Iterator<ServprodTable> iterator = servprodTableList.iterator(); iterator.hasNext();) {
                root.add(toJson(iterator.next()));
            }
        }

        return root;
    }

	public static ObjectNode toJsonPage(Page<ServprodTable> page) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();
		if (page != null)
		{
			root.put("pageNo", page.getPageNo());
			root.put("pageSize", page.getPageSize());
			root.put("pageTotal", page.getPageTotal());
			root.put("recordTotal", page.getRecordTotal());
			root.put("dtoLists", toJson(page.getDtoList()));
		}
		return root;
	}

}
